//
//  SEGBasicNavigationController.m
//  SEGBasicBussiness
//
//  Created by 刘山 on 2018/3/19.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import "SEGBasicNavigationController.h"

#if __has_include(<Commom/UIImage+YYAdd.h>)
#import <Commom/UIImage+YYAdd.h>
#else
#import "UIImage+YYAdd.h"
#endif

#if __has_include(<CommonMediator/SEGMediatorHeader.h>)
#import <CommonMediator/SEGMediatorHeader.h>
#else
#import "SEGMediatorHeader.h"
#endif

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

#if __has_include(<Commom/SEGCommonConfig.h>)
#import <Commom/SEGCommonConfig.h>
#else
#import "SEGCommonConfig.h"
#endif

#if __has_include(<SEGPlatform/SEGPlatformManager.h>)
#import <SEGPlatform/SEGPlatformManager.h>
#else
#import "SEGPlatformManager.h"
#endif

#if __has_include(<Commom/UIColor+YYAdd.h>)
#import <Commom/UIColor+YYAdd.h>
#else
#import "UIColor+YYAdd.h"
#endif

#if __has_include(<Commom/UIImage+SEGTheme.h>)
#import <Commom/UIImage+SEGTheme.h>
#else
#import "UIImage+SEGTheme.h"
#endif


@interface SEGBasicNavigationController () <UIGestureRecognizerDelegate>

@end

@implementation SEGBasicNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置右滑返回手势的代理为自身
    __weak typeof(self) weakself = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = (id)weakself;
    }
    
    // 设置导航栏样式
    if ([SEGCommonConfig shared].navBarAppearanceConfigBlock) {
        [SEGCommonConfig shared].navBarAppearanceConfigBlock(self);
    } else {
        if (@available(iOS 13.0, *)) {
            //神奇的问题
            //36521 【hi居v2.4.1】iOS 邻里圈、邻里详情右上角背景色展示不正确
            UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
            appearance.backgroundColor = kColor_W;
            self.navigationBar.standardAppearance = appearance;
        }
        [UINavigationBar appearance].barTintColor = kColor_W;
        [UINavigationBar appearance].translucent = NO;
        [self.navigationBar setBackgroundImage:[UIImage imageWithColor:kColor_W] forBarMetrics:UIBarMetricsDefault];

        [self.navigationBar setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16], NSForegroundColorAttributeName:kColor_1}];
    }
}

#pragma mark - UIGestureRecognizerDelegate
//这个方法是在手势将要激活前调用：返回YES允许右滑手势的激活，返回NO不允许右滑手势的激活
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        //屏蔽调用rootViewController的滑动返回手势，避免右滑返回手势引起死机问题
        if (self.viewControllers.count < 2 ||
            self.visibleViewController == [self.viewControllers objectAtIndex:0]) {
            return NO;
        }
    }
    //这里就是非右滑手势调用的方法啦，统一允许激活
    return YES;
}

/**
 * push时候隐藏底部工具栏
 * @author Sam
 */
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    //push 的时候隐藏Tab
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        //隐藏活动气泡
        [[SEGUhomeMediator sharedInstance] seg_hiddenActivitySignBubble:nil];
        [[SEGUhomeMediator sharedInstance] seg_hiddenActivityLotteryBubble:nil];
    }
    
    [super pushViewController:viewController animated:animated];
}

-(UIViewController *)popViewControllerAnimated:(BOOL)animated{
    return [super popViewControllerAnimated:animated];
}

-(NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated{
    return [super popToViewController:viewController animated:animated];
}

-(void)goBack:(id)sender{
    [self popViewControllerAnimated:YES];
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [super traitCollectionDidChange:previousTraitCollection];
    //处理返回按钮
    for (UIViewController *vc in self.viewControllers) {
        if (vc.navigationItem.leftBarButtonItem && [vc.navigationItem.leftBarButtonItem.customView isKindOfClass:[UIButton class]]) {
            UIButton *backBtn = (UIButton *)vc.navigationItem.leftBarButtonItem.customView;
            UIImage* image = [UIImage imageWithContentsOfFile:backBtn.imageView.image.anyNamePath];
            [backBtn setImage:image forState:UIControlStateNormal];
        }
    }
    
    
    if ([SEGCommonConfig shared].navBarAppearanceConfigBlock) {
        return;
    }
    if (@available(iOS 13.0, *)) {
        self.navigationBar.standardAppearance.backgroundColor = kColor_W;
    }
}

@end
